const db = wx.cloud.database()
var util = require('../../utils/util.js')
Page({
  data: {
    fileslist: {}
  },

  onLoad: function(options) {
    db.collection('files').orderBy('istop', 'desc').orderBy('submitdate', 'desc').get().then(res => {
      for (var index in res.data) {
        res.data[index].time = util.formatTime(res.data[index].submitdate)
      }
      this.setData({
        fileslist: res.data
      })
    })
  },

  downloadFile: function(event) {
    wx.showLoading({
      title: '文档打开中...',
    })
    wx.cloud.downloadFile({
      fileID: event.currentTarget.dataset.id
    }).then(res => {
      console.log(res.tempFilePath)
      const filePath = res.tempFilePath
      wx.openDocument({
        filePath: filePath,
        success: res => {
          console.log('打开文档成功')
          wx.hideLoading()
        }
      })
    }).catch(error => {
      // handle error
    })
  },
})